/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.erossi.pigest.web.iva;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.erossi.pigest.model.iva.BollatoIva;
import org.springframework.web.servlet.view.document.AbstractExcelView;

/**
 * 'ListaBollatiIvaHSSFView' class: classe che costituisce la vista per la versione HSSF
 *   della lista dei bollati IVA.
 *
 * @author erossi
 * @version Dic 1, 2009
 */
public class ListaBollatiIvaHSSFView extends AbstractExcelView {

  @Override
  @SuppressWarnings("unchecked")
  protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
    
    HSSFCellStyle dateCellStyle = workbook.createCellStyle();
    dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));

    HSSFSheet sheet_0 = workbook.createSheet(this.getApplicationContext().getMessage("bean.vat_journals", null, request.getLocale()));

    HSSFRow row_0 = sheet_0.createRow(0);

    HSSFCell cell_r0c0 = row_0.createCell((short) 0);
    cell_r0c0.setCellValue(this.getApplicationContext().getMessage("bean.vat_journals.id", null, request.getLocale()));
    HSSFCell cell_r0c1 = row_0.createCell((short) 1);
    cell_r0c1.setCellValue(this.getApplicationContext().getMessage("bean.vat_journals.version", null, request.getLocale()));
    HSSFCell cell_r0c2 = row_0.createCell((short) 2);
    cell_r0c2.setCellValue(this.getApplicationContext().getMessage("bean.vat_journals.code", null, request.getLocale()));
    HSSFCell cell_r0c3 = row_0.createCell((short) 3);
    cell_r0c3.setCellValue(this.getApplicationContext().getMessage("bean.vat_journals.description", null, request.getLocale()));
    HSSFCell cell_r0c4 = row_0.createCell((short) 4);
    cell_r0c4.setCellValue(this.getApplicationContext().getMessage("bean.vat_journals.valid_from", null, request.getLocale()));
    HSSFCell cell_r0c5 = row_0.createCell((short) 5);
    cell_r0c5.setCellValue(this.getApplicationContext().getMessage("bean.vat_journals.valid_until", null, request.getLocale()));
    HSSFCell cell_r0c6 = row_0.createCell((short) 6);
    cell_r0c6.setCellValue(this.getApplicationContext().getMessage("bean.vat_journals.sales", null, request.getLocale()));
    HSSFCell cell_r0c7 = row_0.createCell((short) 7);
    cell_r0c7.setCellValue(this.getApplicationContext().getMessage("bean.vat_journals.buy", null, request.getLocale()));
    
    List<BollatoIva> bollatiIva = (List<BollatoIva>) model.get("listaBollatiIva");
    
    if (bollatiIva != null) {
    
      for (int i = 0; i < bollatiIva.size(); i++) {
        
        BollatoIva bollatoIva_i = bollatiIva.get(i);
        
        HSSFRow row_i = sheet_0.createRow(i + 1);
        
        HSSFCell cell_rIc0 = row_i.createCell((short) 0);
        Long id = bollatoIva_i.getId();
        if (id != null) {cell_rIc0.setCellValue(id);}
        
        HSSFCell cell_rIc1 = row_i.createCell((short) 1);
        Long version = bollatoIva_i.getVersion();
        if (version != null) {cell_rIc1.setCellValue(version);}
        
        HSSFCell cell_rIc2 = row_i.createCell((short) 2);
        String codice = bollatoIva_i.getCodice();
        if (codice != null) {cell_rIc2.setCellValue(codice);}

        HSSFCell cell_rIc3 = row_i.createCell((short) 3);
        String descrizione = bollatoIva_i.getDescrizione();
        if (descrizione != null) {cell_rIc3.setCellValue(descrizione);}

        HSSFCell cell_rIc4 = row_i.createCell((short) 4);
        cell_rIc4.setCellStyle(dateCellStyle);
        Calendar dataInizioValidita = bollatoIva_i.getDataInizioValidita();
        if (dataInizioValidita != null) {cell_rIc4.setCellValue(dataInizioValidita);} 
        
        HSSFCell cell_rIc5 = row_i.createCell((short) 5);
        cell_rIc5.setCellStyle(dateCellStyle);
        Calendar dataFineValidita = bollatoIva_i.getDataFineValidita();
        if (dataFineValidita != null) {cell_rIc5.setCellValue(dataFineValidita);} 
        
        HSSFCell cell_rIc6 = row_i.createCell((short) 6);
        Boolean vendite = bollatoIva_i.getVendite();
        if (vendite != null) {
          if (vendite) {
            cell_rIc6.setCellValue(this.getApplicationContext().getMessage("general.enabled", null, request.getLocale()));
          } else {
            cell_rIc6.setCellValue(this.getApplicationContext().getMessage("general.disabled", null, request.getLocale()));
          }
        }
        
        HSSFCell cell_rIc7 = row_i.createCell((short) 7);
        Boolean acquisti = bollatoIva_i.getAcquisti();
        if (acquisti != null) {
          if (acquisti) {
            cell_rIc7.setCellValue(this.getApplicationContext().getMessage("general.enabled", null, request.getLocale()));
          } else {
            cell_rIc7.setCellValue(this.getApplicationContext().getMessage("general.disabled", null, request.getLocale()));
          }
        }
        
      }
    
    }
    
  }
  
}

